package com.example.demo.controller;

import com.example.demo.model.common.RenewalRequest;
import com.example.demo.model.common.RenewalResponse;
import com.example.demo.model.common.RenewalStatusResponse;
import com.example.demo.model.entity.CardDetail;
import com.example.demo.model.entity.CardInfo;
import com.example.demo.model.entity.CardPackage;
import com.example.demo.service.IotCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/iot-card")
public class IotCardController {

    @Autowired
    private IotCardService iotCardService;

    @GetMapping("/card-info")
    public List<CardInfo> getCardInfo(@RequestParam(defaultValue = "898608141623D0224428") String iccid) {
        return iotCardService.queryCardInfoList(Collections.singletonList(iccid));
    }

    @GetMapping("/card-detail")
    public List<CardDetail> getCardDetail(@RequestParam(defaultValue = "898608141623D0224428") String iccid) {
        return iotCardService.queryCardDetail(Collections.singletonList(iccid));
    }

    @GetMapping("/card-package")
    public List<CardPackage> getCardPackage(@RequestParam(defaultValue = "898608141623D0224428") String iccid) {
        // 检查iccid是否包含多个值（逗号分隔）
        if (iccid.contains(",")) {
            List<String> iccidList = Arrays.stream(iccid.split(","))
                    .map(String::trim)
                    .collect(Collectors.toList());
            return iotCardService.queryCardPackage(iccidList);
        }
        return iotCardService.queryCardPackage(Collections.singletonList(iccid));
    }

    /**
     * 批量查询卡套餐（通过POST方法，支持大批量ICCID）
     */
    @PostMapping("/card-package/batch")
    public List<CardPackage> batchGetCardPackage(@RequestBody Map<String, List<String>> request) {
        List<String> iccids = request.get("iccids");
        if (iccids == null || iccids.isEmpty()) {
            throw new IllegalArgumentException("iccid列表不能为空");
        }
        return iotCardService.queryCardPackage(iccids);
    }

    /**
     * 批量续费
     * 注意：批量续费卡数量单次不能超过150张
     * 同一批卡必须使用同一个资费计划
     * 获取到codeUrl后请在30分钟内付款，否则需要重新请求
     */
    @PostMapping("/renewal")
    public RenewalResponse cardRenewal(@RequestBody RenewalRequest renewalRequest) {
        // 验证请求参数
        if (renewalRequest.getIccid() == null || renewalRequest.getIccid().isEmpty()) {
            throw new IllegalArgumentException("iccid列表不能为空");
        }
        if (renewalRequest.getPackageId() == null || renewalRequest.getPackageId().isEmpty()) {
            throw new IllegalArgumentException("packageId不能为空");
        }
        if (renewalRequest.getPacketId() == null || renewalRequest.getPacketId().isEmpty()) {
            throw new IllegalArgumentException("packetId不能为空");
        }
        if (renewalRequest.getValidateType() == null || renewalRequest.getValidateType().isEmpty()) {
            throw new IllegalArgumentException("validateType不能为空");
        }

        return iotCardService.mulCardRenewal(renewalRequest);
    }

    /**
     * 查询续费审核状态
     * 使用批量续费接口返回的renewalId进行查询
     */
    @PostMapping("/renewal-status")
    public RenewalStatusResponse queryRenewalStatus(@RequestBody Map<String, String> request) {
        String renewalId = request.get("renewalId");
        if (renewalId == null || renewalId.isEmpty()) {
            throw new IllegalArgumentException("renewalId不能为空");
        }
        return iotCardService.queryRenewalStatus(renewalId);
    }
}